package com.meida.student.app.scholartyrant.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.SaveInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusOrderAnswer;
import com.meida.module.bus.client.enums.OrderStatePEnum;
import com.meida.module.bus.provider.service.BusOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("SOrderAnswerSaveHandler")
public class SOrderAnswerSaveHandler implements SaveInterceptor<BusOrderAnswer> {
    @Autowired
    private BusOrderService busOrderService;

    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap params) {
        BusOrderAnswer busOrderAnswer = cs.getEntity(BusOrderAnswer.class);
        Long orderId = busOrderAnswer.getOrderId();
        if (FlymeUtils.isEmpty(orderId)) {
            return ResultBody.failed("缺少订单参数");
        }
        String answerContent = busOrderAnswer.getAnswerContent();
        if (FlymeUtils.isEmpty(answerContent)) {
            return ResultBody.failed("缺少文字解答");
        }
        String fileIds = cs.getParams("fileIds");
        if (FlymeUtils.isEmpty(fileIds)) {
            return ResultBody.failed("缺少讲解视频");
        }
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaSave criteriaSave, EntityMap entityMap, BusOrderAnswer busOrderAnswer) {

    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusOrderAnswer entity) {
        Long orderId = entity.getOrderId();
        BusOrder busOrder = busOrderService.getById(orderId);
        busOrder.setOrderState(OrderStatePEnum.OrderState4.getValue());
        busOrderService.updateById(busOrder);
    }

}
